<template>
  <div
    class="vaw-verify-container"
    ref="container"
  >
    <div
      class="drag-bg"
      :class="{'reset-width-anim': resetStatus}"
      :style="{'width': lockStyle.left + 'px'}"
    ></div>
    <span
      ref="lockBtn"
      class="lock-btn iconfont"
      :class="{'reset-left-anim': resetStatus}"
      :style="{
        'left': lockStyle.left + 'px', 
        'color': verifySuccess ? '#7ac23c' : '#737383',
        'font-size': verifySuccess ? '20px' : '26px'
      }"
      @touchstart="onMouseDown"
      @mousedown="onMouseDown"
    >{{ verifySuccess ? '&#xe63c;' : '&#xe6c4;'}}</span>
    <div
      class="text-tip"
      :class="{'mask-slide' : !verifySuccess}"
      :style="{'color': verifySuccess ? '#ffffff' : '#333333'}"
    >{{ textTip }}</div>
  </div>
</template>
<script>
export default {
  name: 'VawVerifySimple',
  data() {
    return {
      lockStyle: {
        left: 0
      },
      maxWidth: 0,
      startX: 0,
      endX: 0,
      resetStatus: false,
      verifySuccess: false,
      textTip: '请按住滑块，拖动到最右边'
    }
  },
  mounted() {
    this.maxWidth = this.$refs.container.clientWidth - this.$refs.lockBtn.clientWidth - 2
    this.addEventListener()
  },
  unmounted() {
    this.removeEventListener()
  },
  methods: {
    onMouseDown(e) {
      if (this.verifySuccess) return
      e.preventDefault()
      this.resetStatus = false
      this.startX = e.clientX || e.changedTouches[0].clientX
      this.canMove = true

    },
    onMouseMove(e) {
      if (!this.canMove || this.verifySuccess) return
      this.endX = e.clientX || e.changedTouches[0].clientX
      const x = (this.endX || e.changedTouches[0].clientX) - this.startX
      this.lockStyle.left = Math.max(0, Math.min(x, this.maxWidth))
      if (this.lockStyle.left >= this.maxWidth) {
        this.handleSuccess()
      }
    },
    onMouseUp() {
      if (this.verifySuccess) return
      this.$emit('verify-fail')
      this.canMove = false
      this.resetStatus = true
      this.lockStyle.left = 0
    },
    handleSuccess() {
      this.verifySuccess = true
      this.textTip = '验证通过'
      this.removeEventListener()
      this.$emit('verify-success')
    },
    reset() {
      this.addEventListener()
      this.resetStatus = true
      this.verifySuccess = false
      this.canMove = false
      this.textTip = '请按住滑块，拖动到最右边'
      this.lockStyle.left = 0
    },
    addEventListener() {
      document.getElementsByTagName('html')[0].addEventListener('mousemove', this.onMouseMove)
      document.getElementsByTagName('html')[0].addEventListener('mouseup', this.onMouseUp)
      document.getElementsByTagName('html')[0].addEventListener('touchmove', this.onMouseMove)
      document.getElementsByTagName('html')[0].addEventListener('touchend', this.onMouseUp)
    },
    removeEventListener() {
      document.getElementsByTagName('html')[0].removeEventListener('mousemove', this.onMouseMove)
      document.getElementsByTagName('html')[0].removeEventListener('mouseup', this.onMouseUp)
      document.getElementsByTagName('html')[0].removeEventListener('touchmove', this.onMouseMove)
      document.getElementsByTagName('html')[0].removeEventListener('touchend', this.onMouseUp)
    }
  }
}
</script>

<style>
@keyframes masked-animation {
  0% {
    background-position: 400% 0;
  }
  100% {
    background-position: -400% 0;
  }
}
</style>

<style scoped>
.vaw-verify-container {
  position: relative;
  width: 100%;
  height: 38px;
  background-color: #e8e8e8;
}
.lock-btn {
  position: absolute;
  top: 0;
  left: 0;
  display: inline-block;
  background-color: #ffffff;
  box-sizing: border-box;
  border: 1px solid #e8e8e8;
  height: 100%;
  width: 42px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 26px;
  color: #737383;
  cursor: move;
  z-index: 9;
}
.reset-left-anim {
  left: 0;
  transition: left 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}
.reset-width-anim {
  width: 0;
  transition: width 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}
.drag-bg {
  width: 0;
  height: 100%;
  background-color: #7ac23c;
}
.text-tip {
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  text-align: center;
  color: #333333;
  font-size: 12px;
  line-height: 38px;
  font-weight: bold;
}
.mask-slide {
  background: -webkit-gradient(
    linear,
    left top,
    right top,
    color-stop(0, #4d4d4d),
    color-stop(0.4, #4d4d4d),
    color-stop(0.5, #fff),
    color-stop(0.6, #4d4d4d),
    color-stop(1, #4d4d4d)
  );
  -webkit-text-fill-color: transparent;
  -webkit-background-clip: text;
  -webkit-background-size: 200% 100%;
  -webkit-animation: masked-animation 10s infinite linear;
}
@font-face {
  font-family: "iconfont";
  src: url('../../assets/iconfont/iconfont.woff2?t=1620349351905') format('woff2'),
       url('../../assets/iconfont/iconfont.woff?t=1620349351905') format('woff'),
       url('../../assets/iconfont/iconfont.ttf?t=1620349351905') format('truetype');
}

.iconfont {
  font-family: "iconfont" !important;
  font-size: 16px;
  font-style: normal;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

.icon-success:before {
  content: "\e63c";
}

.icon-shuangjiantou-you:before {
  content: "\e6c4";
}

</style>
